home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 187_01 / ltoa.c < prev    next >
Text File  |  1985-12-28  |  1KB  |  36 lines

  1. /*@*****************************************************/
  2. /*@                                                    */
  3. /*@ ltoa - convert long to ascii.                      */
  4. /*@        Very useful to prevent loading of printf    */
  5. /*@        and the 2-8K of follow-ons.                 */
  6. /*@                                                    */
  7. /*@   Usage:     ltoa(num, buffer);                    */
  8. /*@       where num is a long.                         */
  9. /*@             buffer is a char area large enough to  */
  10. /*@                to hold the resulting string.       */
  11. /*@                                                    */
  12. /*@   Returns a pointer to the buffer.  This allows    */
  13. /*@       nesting of puts(ltoa(n, buffer)); variety.   */
  14. /*@                                                    */
  15. /*@*****************************************************/
  16.  
  17. char *ltoa(n,s)        /* convert n to characters in s */
  18. char s[];
  19. long n;
  20. {
  21.     long sign;
  22.     int i;
  23.  
  24.     if ((sign = n) < 0)  /* record sign */
  25.         n = -n;         /* make positive */
  26.     i = 0;
  27.     do {                /* generate digits in reverse order */
  28.         s[i++] = n % 10 + '0';          /* get next digit */
  29.     } while ((n /= 10) > 0);        /* delete it */
  30.     if (sign < 0)
  31.         s[i++] = '-';
  32.     s[i] = '\0';
  33.     reverse(s);
  34.     return s;
  35. }
  36.